package com.tgy.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/***
 * @ClassName: MyBatisUtil
 * @Description: 工具类
 * @Auther: 送一一块二向箔
 * @Date: 14:53 2021/12/14
 * @version : V1.0
 */
//SqlSessionFactory --> 构建SqlSession
public class MyBatisUtil {
    private static SqlSessionFactory sqlSessionFactory;
    static{
        try {
            //获取SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
}
